<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="el-form">
            <el-form-item label="关联用户:" prop="personNo">
                <el-select v-model="ruleForm.personNo" class="w100p" clearable filterable @change="personData(ruleForm)">
                    <el-option v-for="items in person" :key="items.person.personNo" :label="items.person.name"
                        :value="items.person.personNo" />
                </el-select>
            </el-form-item>
            <el-form-item label="联系人名称:" prop="contactsName">
                <el-input v-model="ruleForm.contactsName" class="w100p" clearable placeholder="请输入" />
            </el-form-item>
            <el-form-item label="手机号:" prop="phone">
                <el-input v-model="ruleForm.phone" class="w100p" clearable placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="微信:" prop="weChat">
                <el-input v-model="ruleForm.weChat" class="w100p" clearable placeholder="请输入"/>
            </el-form-item>
            <el-form-item label="邮箱:" prop="email">
                <el-input v-model="ruleForm.email" class="w100p" clearable placeholder="请输入"/>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import notificationContactManagement from '@/api/system-notification-management/notification-contact-management.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                contactsName: [
                    {
                        required: true,
                        message: "请填写姓名",
                        trigger: ["blur", "change"],
                    },
                ],
                personNo: [
                    {
                        required: true,
                        message: "请选择用户",
                        trigger: ["blur", "change"],
                    },
                ],
                phone: [
                    {
                        required: true,
                        message: "手机号不能为空",
                        trigger: "blur",
                    },
                    {
                        pattern:
                            /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
                        message: "请输入正确电话号码",
                    },
                ],
                email: [
                    {
                        message: "请输入邮箱地址",
                        trigger: ["blur", "change"],
                    },
                    {
                        pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
                        message: "请输入正确的邮箱地址",
                    },
                ],
                weChat: [
                    {
                        message: "请输入微信号",
                        trigger: ["blur", "change"],
                    },
                    {
                        pattern: /^[a-zA-Z][a-zA-Z\d_-]{5,19}$/,
                        message: "请输入正确的微信号",
                    },
                ],
            }
        }
    },
    computed: {
        person() {
            return this.$store.state.initData.person || [];
        }
    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = notificationContactManagement.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = notificationContactManagement.edit
                this.addEditName = '编辑'
            }
        },
        personData(val) {
            this.person.forEach((item) => {
                if (val.personNo === item.personNo) {
                    this.$set(this.ruleForm, "contactsName", item.person.name);
                }
            });
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  